import 'package:base_flutter/base_flutter.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/util/ext_utils.dart';

import '../../a_elder/models/elder_entity.dart';
import '../../a_elder/page/elder_care_send_page.dart';
import '../../a_elder/widget/elder_info_title_cell.dart';
import '../../login/page/user_manager.dart';
import '../../net/api/http_api.dart';
import '../../res/colors.dart';
import '../../res/gaps.dart';
import '../../res/styles.dart';
import '../../viewmodel/nurse/nurse_record_detail_view_model.dart';
import '../../widgets/click_item.dart';

class NurseRecordDetailPage extends BaseStatefulMvvmWidget {
  final dynamic detailId;
  final ElderInfo elderInfo;

  NurseRecordDetailPage({required this.detailId, required this.elderInfo});

  @override
  State<StatefulWidget> createState() => _NurseRecordDetailState();
}

class _NurseRecordDetailState
    extends BaseMvvmState<NurseRecordDetailViewModel, NurseRecordDetailPage> {
  @override
  Widget buildLoadingContentView() {
    return Column(
      children: [
        ListView(
          children: [
            ElderInfoTitleCell(
              elderInfo: widget.elderInfo,
            ),
            Gaps.vGap16,
            ClickItem(
              title: '发生时间：',
              titleStyle: TextStyles.textDark16,
              contentStyle: TextStyles.textDark16,
              content: viewModel.detail?.bean?.recordTime ?? "",
            ),
            Container(
              padding: EdgeInsets.all(10),
              margin: EdgeInsets.all(20),
              alignment: Alignment.topLeft,
              constraints: BoxConstraints(minHeight: 140),
              decoration: BoxDecoration(
                  color: Colours.bg_color, borderRadius: 8.borderRadius),
              child: CommonText(viewModel.detail?.bean?.content ?? ""),
            ),
            Container(
              margin: const EdgeInsets.only(left: 15.0),
              padding: EdgeInsets.fromLTRB(0, 5, 20, 5),
              decoration: BoxDecoration(
                border: Border(
                  bottom: Divider.createBorderSide(
                    context,
                    color: Colours.line,
                    width: 1,
                  ),
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Expanded(
                    child: Text(
                      '是否列入交班',
                      style: TextStyle(color: Colours.text_dark, fontSize: 16),
                    ),
                  ),
                  Icon(1 == viewModel.detail?.bean?.isShift
                      ? Icons.radio_button_checked
                      : Icons.radio_button_off),
                  Container(
                    width: 10,
                  ),
                  Text('是'),
                  Container(
                    width: 10,
                  ),
                  Icon(0 == viewModel.detail?.bean?.isShift
                      ? Icons.radio_button_checked
                      : Icons.radio_button_off),
                  Container(
                    width: 10,
                  ),
                  Text('否'),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(left: 15.0),
              padding: EdgeInsets.fromLTRB(0, 15, 20, 15),
              decoration: BoxDecoration(
                border: Border(
                  bottom: Divider.createBorderSide(
                    context,
                    color: Colours.line,
                    width: 1,
                  ),
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Expanded(
                    child: Text(
                      '特别通知人',
                      style: TextStyle(color: Colours.text_dark, fontSize: 16),
                    ),
                  ),
                  CommonText(viewModel.detail?.bean?.notifier)
                ],
              ),
            ),
            GridView.count(
              crossAxisCount: 4,
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              children: (viewModel.detail?.bean?.attachList ?? []).map((e) {
                return CachedNetworkImage(
                  width: (ScreenUtil.getScreenW(context) - 32 - 20) / 4,
                  height: (ScreenUtil.getScreenW(context) - 32 - 20) / 4,
                  fit: BoxFit.cover,
                  imageUrl: '${HttpApi.imageDownLoadUrl}${e.id}',
                  httpHeaders: {"Cookie":UserManager.cookieStr},
                  placeholder: (context, url) => new ProgressView(),
                  errorWidget: (context, url, error) => new Icon(
                    Icons.error,
                    color: Colors.black,
                  ),
                ).toRound(borderRadius: 6.borderRadius).onTap(() {
                  var list = (viewModel.detail?.bean?.attachList ?? [])
                      .map((image) => "${HttpApi.imageDownLoadUrl}${image.id}")
                      .toList();
                  var path = "${HttpApi.imageDownLoadUrl}${e.id}";
                  Go().push(ShowImagePage(
                    list,
                    image: path,
                  ));
                });
              }).toList(),
              padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
              crossAxisSpacing: 10,
              mainAxisSpacing: 10,
            )
          ],
        ).setWeight(1),
        TextButton(
          onPressed: () async {
            var data = await Go().push(ElderCareSendPage(elderInfo: widget.elderInfo,detail: viewModel.detail,customerId: widget.elderInfo.customerId,));
            viewModel.getDetail();
          },
          child: CommonText('修改', textColor: Colors.red),
          style: ButtonStyle(
              shape: WidgetStateProperty.all(
                  StadiumBorder(side: BorderSide(color: Colors.red))),
              minimumSize: WidgetStateProperty.all(
                  Size(context.screenSize.width - 32, 44))),
        ),
        SizedBox(
          height: 20,
        )
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text('照护详情'),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  NurseRecordDetailViewModel createViewModel() {
    return NurseRecordDetailViewModel(context, detailId: widget.detailId);
  }

  @override
  void onRetryClick() {}
}
